export class nl extends plugin {
  constructor() {
    super({
      name: "石头剪刀布",
      dsc: "经典猜拳游戏",
      event: "message",
      priority: 5000,
      rule: [{
        reg: "^#(石头剪刀布|rps)(\\s+)?(石头|剪刀|布)?$",
        fnc: "rpsGame"
      }]
    })
    this.rpsMap = {
      石头: 0,
      剪刀: 1,
      布: 2
    }
  }

  async rpsGame() {
    const input = this.e.msg.match(/^#(?:石头剪刀布|rps)\s?(.*)/)[1]
    if (!input) return this.reply("请出拳！格式：#石头剪刀布 [石头/剪刀/布]")
    
    try {
      const userChoice = this.validate(input)
      const botChoice = Math.floor(Math.random() * 3)
      
      const result = this.judge(userChoice, botChoice)
      const msg = [
        `✊ 你出了：${this.getKey(userChoice)}`,
        `🤖 我出了：${this.getKey(botChoice)}`,
        this.formatResult(result)
      ]
      
      this.reply(msg.join("\n"))
    } catch {
      this.reply("出拳无效！只能出石头/剪刀/布哦~")
    }
  }

  validate(input) {
    const choice = this.rpsMap[input.trim()]
    if (choice === undefined) throw new Error()
    return choice
  }

  judge(user, bot) {
    if (user === bot) return 0
    return ((user - bot) + 3) % 3 === 1 ? -1 : 1
  }

  getKey(val) {
    return Object.keys(this.rpsMap).find(k => this.rpsMap[k] === val)
  }

  formatResult(result) {
    const results = {
      1: "🎉 你赢了！好厉害哦 (≧▽≦)",
      0: "✨ 平局！要不再来一局？",
      [-1]: "😭 你输了，再接再厉啦～"
    }
    return results[result] + this.getEmoji(result)
  }

  getEmoji(result) {
    const emojis = {
      1: " 🎊", 
      0: " 🤝",
      [-1]: " 💢"
    }
    return emojis[result]
  }
}